<?php
namespace app\model;

use app\model\CourierInfo as ModelCourierInfo;
use app\model\StationInfo as ModelStationInfo;
use app\model\DriverInfo as ModelDriverInfo;

use think\Db;

class SysSetting extends Common
{
    protected static $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->db_name = 'sys_setting';
    }

    /**
     * 类入口
     * @return SysSetting
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    public function getValue($key)
    {
        return Db::name($this->db_name)->where(['key'=>$key])-> value('value');

    }

    /*
    * 返回提现手续费
    * URI:/public/procedure.fee
    */
    public function getProcedureFee($money)
    {
//        $money = $this->_getContentParams('money');
        _toValidate('money',$money,11007);

        $fee = $this->getValue('base_procedure_fee'); //手续费基数
        $percent = $this->getValue('procedure_percent'); //手续费%

        $serverCharge = sprintf("%.2f",$fee+$money*$percent/100);
        return $serverCharge;
    }

    /*
     * 返回快递员分佣类型
     */
    public function courierCharges($type)
    {
        switch ($type){
            case 1 :
                $value = Db::name($this->db_name)->where(['key'=>'courier_charges_piece'])-> value('value');
                break;
            case 2 :
                $value = Db::name($this->db_name)->where(['key'=>'courier_charges_percent'])-> value('value');
                break;
            default:
                _returnError(10014);
                break;
        }
        return $value;
    }
    /*
    * 返回站点分佣类型
    */
    public function stationCharges($type)
    {
        switch ($type){
            case 1 :
                $value = Db::name($this->db_name)->where(['key'=>'station_charges_piece'])-> value('value');
                break;
            case 2 :
                $value = Db::name($this->db_name)->where(['key'=>'station_charges_percent'])-> value('value');
                break;
            default:
                _returnError(10014);
                break;
        }
        return $value;
    }

    /*
     * 获取分佣类型并结算
     * mapType: 1：快递员；2：司机；3站点
     * mapId:对应快递员，站点，司机id
     */
    public function commissionAdd($mapType,$mapId,$orderId)
    {
        switch ($mapType){
            case 1:
                $obj = ModelCourierInfo::getInstance();
                break;
            case 2:
                $obj = ModelDriverInfo::getInstance();
                break;
            case 3:
                $obj = ModelStationInfo::getInstance();
                break;
            default :
                _returnError(10014);
                break;
        }
    }
}
